package com.demo.test.other;

import org.junit.Test;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author gy
 * @date 2024/3/18
 */
public class Dice {
    private final static Integer COUNT = 10000;
    @Test
    public void m1(){
        HashMap<String, Integer> map = new HashMap<>();
        for (int i = 0; i < COUNT; i++) {
            int r1 = (int)(Math.floor(Math.random()*6+1));
            int r2 = (int)(Math.floor(Math.random()*6+1));
            int r3 = (int)(Math.floor(Math.random()*6+1));
            int sum =  r1 + r2 + r3;
            String res = null;
            if (r1 == r2 && r2 == r3){
                res = "通吃";
            }else if (sum >= 11 && sum <= 17){
                res = "大";
            }else if (sum >= 4 && sum <= 10){
                res = "小";
            }
            if (map.get(res) == null) {
                map.put(res,1);
            }else{
                map.put(res,(int)map.get(res) + 1);
            }
            System.out.println("第"+(i+1)+"局: "+ + r1 + " " + r2 + " " + r3 + "---" +res);

        }
        System.out.println(map);
        System.out.println("统计结果：");
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            String res = entry.getKey();
            Integer num = entry.getValue();
            BigDecimal dividendBigDecimal = new BigDecimal(num).multiply(new BigDecimal(100));
            // 将除数转换为BigDecimal类型
            BigDecimal divisorBigDecimal = new BigDecimal(COUNT);
            // 使用BigDecimal进行除法计算，并指定保留两位小数和舍入方式
            BigDecimal resultBigDecimal = dividendBigDecimal.
                    divide(divisorBigDecimal, 2, RoundingMode.HALF_UP);
            System.out.println(res + "  占比"+ resultBigDecimal+ "%");
        }
    }
}
